using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcExtExample.Models;

namespace MvcExtExample.Controllers
{
    public class DeptManageController : Controller
    {
        public ActionResult addDept(string txtDepID, string txtDepName, string selectParentDepID)
        {
            bool result = addDeptDAL(txtDepID, txtDepName, selectParentDepID);
            if (result)
                return Content("插入成功！");
            else
                return Content("插入失败！");

        }

        private bool addDeptDAL(string txtDepID, string txtDepName, string selectParentDepID)
        {
            bool result = false;
            //连接数据库
            SqlConnection ts = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            //打开数据库连接
            ts.Open();
            //初始化查询语句
            SqlCommand it = new SqlCommand("INSERT INTO [dbo].[Tb_Department]( [Dep_ID] ,[Dep_Name] ,[ParentDep_ID])VALUES  ( '" + txtDepID + "','" + txtDepName + " ', '" + selectParentDepID + "')", ts);
            //对SQL Server数据库执行一个transact-SQL语句
            int rows = it.ExecuteNonQuery();
            if (rows > 0)
                result = true;
            //关闭数据库连接
            ts.Close();
            return result;
        }
        //
        // GET: /DeptManage/addDepartment

        public ActionResult addDepartment()
        {
            return View();
        }

        //
        // GET: /DeptManage/departmentManage

        public ActionResult departmentManage()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult DeptsQuery()
        {
            List<Department> litestModel = new List<Department>();
            framework.JSONHelper jsonHelp = new framework.JSONHelper();
            int TotalRecords = 0;
            string strSqlStr = "SELECT TOP 20 [Dep_ID],[Dep_Name],[ParentDep_ID] FROM [Oil_Water_System].[dbo].[Tb_Department]";
            framework.SqlHelperCS.SqlHelper cs = new framework.SqlHelperCS.SqlHelper();
            DataSet DSet = cs.GetDataSet(strSqlStr);
            if (DSet != null)
            {
                DataTable DTable = DSet.Tables[0];
                for (int i = 0; i < DTable.Rows.Count; i++)
                {
                    DataRowView rowview = DSet.Tables[0].DefaultView[i];
                    litestModel.Add(new Department()
                    {
                        //active = true,//默认为勾选状态
                        Dep_ID = rowview["Dep_ID"].ToString(),
                        Dep_Name = rowview["Dep_Name"].ToString(),
                        ParentDep_ID = rowview["ParentDep_ID"].ToString()
                    });
                }
                return Json(litestModel, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return null;
            }
        }

    }
}
